﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace EpcTagLibrary.Pat
{
    public class DOD : Pattern
    {
        private bool m_IsPureId;

        public DOD(PatternField[] fields, Type type)
            : base(fields, type)
        {
        }

        public override string GetScheme()
        {
            return "usdod";
        }

        public override string GetSchemeName()
        {
            return "usdod-96";
        }

        public override int GetTagBitLength()
        {
            return 96;
        }

        public PatternField FilterComponent
        {
            get
            {
                return m_IsPureId ? null : Fields[0];
            }
        }

        public PatternField GmiComponent
        {
            get
            {

                return Fields[m_IsPureId ? 0 : 1];
            }
        }

        public PatternField SerialNumberComponent
        {
            get
            {
                return Fields[m_IsPureId ? 1 : 2];
            }
        }

        public class Factory: PatternFactory
        {
            public override Pattern GetPattern(string patBody, bool isGroupingPattern, bool isPureid)
            {
                if (!patBody.StartsWith("usdod"))
                {
                    throw new PatternParseException("Specified URI does not match the DoD URI for patterns.");
                }

                string[] Fields = patBody.Substring(patBody.IndexOf(":")+1).Split(new char[1] { '.' });

                if ((isPureid && (Fields.Length !=2)) || (!isPureid && (Fields.Length !=3)))
                {
                    throw new PatternParseException("Specified URI does not match the DoD URI for patterns.");
                }

                PatternField[] PatFields = new PatternField[isPureid?2:3];

                try
                {
                    if (!isPureid)
                    {
                        PatFields[0] = new PatternField(Fields[0],isGroupingPattern,4);
                        PatFields[0].Property = typeof(EpcTagLibrary.DOD).GetProperty("Filter");
                    }

                    PatFields[isPureid ?0:1] = new ASCIIPatternField(Fields[isPureid?0:1],isGroupingPattern,48);
                    PatFields[isPureid ? 0 : 1].Property = typeof(EpcTagLibrary.DOD).GetProperty("Gmi");

                    PatFields[isPureid ?1:2] = new PatternField(Fields[isPureid?1:2],isGroupingPattern,36);
                    PatFields[isPureid ? 1 : 2].Property = typeof(EpcTagLibrary.DOD).GetProperty("SerialNumber");
                }
                catch (Exception e)
                {
                    throw new PatternParseException("Error occured setting up pattern component for a field: " + patBody, e);;
                }

                DOD Result = new DOD(PatFields,typeof(EpcTagLibrary.DOD));
                Result.m_IsPureId = isPureid;

                return Result;
            }
        }
    }
}
